﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 条码信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BarCodeInformationController : ControllerBase
    {
        private readonly ILogger<WMSBarCodeInformationRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public BarCodeInformationController(ILogger<WMSBarCodeInformationRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection");

        }

        /// <summary>
        /// WMS条码信息查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostBarCodeInformationSelect([FromBody] WMSBarCodeInformationRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSBarCodeInformationRequestDto result = new WMSBarCodeInformationRequestDto();
            WMSBarCodeInformationResponseDto re = new WMSBarCodeInformationResponseDto();
            string barcode_no = strparm.barcode_no;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select bar.ancestors,bar.barcode_no,bar.barcode_status,bar.source_no,bar.material_code,bar.material_name,bar.material_unit,bar.material_amount,bar.source_batch,bar.supplier_code,bar.supplier_name,bar.warehouse_code,ent.source_no rno,ent.create_by,ent.create_time from 
entry_scan_record ent inner join barcode_file bar on bar.barcode_no=ent.barcode_no where ent.barcode_no='" + barcode_no + "' LIMIT 1");
                DataTable dt_barcodecheck = DB.Query(strSql.ToString(), con);
                if (dt_barcodecheck.Rows.Count<=0)
                {
                    strMsg = "WMS系统不存在《"+ barcode_no + "》条码信息，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel< WMSBarCodeInformationResponseData >(dt_barcodecheck);
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
